import base64

import cv2
import numpy as np

# 将np图片(imread后的图片）转码为base64格式


def cv2_image_to_base64(image_np):
    image = cv2.imencode('.jpg', image_np)[1]
    image_code = str(base64.b64encode(image))[2:-1]

    return image_code


# 将base64编码解析成opencv可用图片


def base64_to_cv2_image(base64_code):
    # base64解码
    img_data = base64.b64decode(base64_code)
    # 转换为np数组
    img_array = np.fromstring(img_data, np.uint8)
    # 转换成opencv可用格式
    img = cv2.imdecode(img_array, cv2.COLOR_RGB2BGR)
    return img


def base64_to_bytes(base64_code):
    # base64解码
    return   base64.b64decode(base64_code)

